<?php

require_once('sisow.cls5.php');

class ControllerPaymentSisowMC extends Controller {
	protected function index() {
		$this->load->language('payment/sisowmc');
		$this->load->model('checkout/order');

		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
		
		$sisow = new Sisow($this->config->get('sisowmc_merchantid'), $this->config->get('sisowmc_merchantkey'));
		//$sisow->DirectoryRequest($arr, false, $this->config->get('sisowmc_testmode') == 'true');
		
		//$this->data['banks'] = $arr;
		$this->data['text_header'] = $this->language->get('text_header');
		$this->data['text_redirect'] = $this->language->get('text_redirect');
		$this->data['text_select'] = $this->language->get('text_select');
		$this->data['button_confirm'] = $this->language->get('button_confirm');

		if (substr(VERSION, 0, 3) == '1.4') {
			$this->data['button_back'] = $this->language->get('button_back');
			if ($this->request->get['route'] != 'checkout/guest_step_3') {
				$this->data['back'] = HTTPS_SERVER . 'index.php?route=checkout/payment';
			}
			else {
				$this->data['back'] = HTTPS_SERVER . 'index.php?route=checkout/guest_step_2';
			}
			$this->id = 'payment';
		}

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/sisowmc.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/payment/sisowmc.tpl';
		} else {
			$this->template = 'default/template/payment/sisowmc.tpl';
		}
		$this->render();
	}

	public function callback() {
		if ($this->request->get['status'] == 'Success') {
			if (substr(VERSION, 0, 3) == '1.4') {
				header("Location: " . HTTPS_SERVER . 'index.php?route=checkout/success');
			}
			else {
				header("Location: " . $this->url->link('checkout/success'));
			}
		} 
		else {
			if (substr(VERSION, 0, 3) == '1.4') {
				header("Location: " . HTTPS_SERVER . 'index.php?route=checkout/guest_step_2'); // or payment??
			}
			else {
				header("Location: " . $this->url->link('checkout/checkout'));
			}
		}
	}

	public function notify() {
		if (isset($this->request->get['trxid'])) {
			$this->load->model('payment/sisowmc');

			$trxid = $this->request->get['trxid'];
			$order_id = $this->request->get['ec'];

			$this->load->model('checkout/order');
			$order_info = $this->model_checkout_order->getOrder($order_id);

			$sisow = new Sisow($this->config->get('sisowmc_merchantid'), $this->config->get('sisowmc_merchantkey'));
			if (($ex = $sisow->StatusRequest($trxid)) < 0) {
				$this->log->write('Sisow MisterCash: StatusRequest ' . $ex . ' ' . $sisow->errorMessage);
				header("Status: 404 Not Found");
				echo 'NOK ' . $ex;
			}
			else {
				if ($sisow->status == 'Success') {
					$this->model_checkout_order->confirm($order_id, $this->config->get('sisowmc_status_success'));
					$message = 'MisterCash transactie ' . $trxid . ' gecontroleerd door Sisow.<br />';
					$message .= 'Bankrekening: ' . $sisow->consumerAccount . '.<br />';
					$message .= 'Ten name van: ' . $sisow->consumerName . '.<br />';
					$message .= 'Plaats: ' . $sisow->consumerCity . '.<br />';
					$this->model_checkout_order->update($order_id, $this->config->get('sisowmc_status_success'), $message, false);
					echo 'OK';
				}
			}
		}
	}

	public function redirectbank() {
		$this->load->language('payment/sisowmc');
		$this->load->model('payment/sisowmc');

		$this->load->model('checkout/order');
		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);

		if (substr(VERSION, 0, 3) == '1.4') {
			$amt = $this->currency->format($order_info['total'], $order_info['currency'], $order_info['value'], false);
		}
		else {
			$amt = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
		}
		if ($this->currency->getCode() != 'EUR') {
			$amt = $this->currency->convert($amt, $this->currency->getCode(), 'EUR');
		}

		$sisow = new Sisow($this->config->get('sisowmc_merchantid'), $this->config->get('sisowmc_merchantkey'));
		$sisow->payment = 'mistercash';
		if ($this->config->get('sisowmc_testmode') == 'true') {
			$sisow->issuerId = '99';
		}
		$sisow->purchaseId = $order_info['order_id'];
		$sisow->description = $this->config->get('config_name') . " order " . $order_info['order_id'];
		$sisow->amount = $amt;
		if (substr(VERSION, 0, 3) == '1.4') {
			//$sisow->amount = $this->currency->format($order_info['total'], $order_info['currency'], $order_info['value'], false);
			$sisow->returnUrl = HTTPS_SERVER . 'index.php?route=payment/sisowmc/callback';
			$sisow->notifyUrl = HTTPS_SERVER . 'index.php?route=payment/sisowmc/notify';
		}
		else {
			//$sisow->amount = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
			$sisow->returnUrl = $this->url->link('payment/sisowmc/callback');
			$sisow->notifyUrl = $this->url->link('payment/sisowmc/notify');
		}
		$json = array();
		if (($ex = $sisow->TransactionRequest()) < 0) {
			$this->log->write('Sisow MisterCash: TransactionRequest ' . $ex . ' ' . $sisow->errorMessage);
			$json['error'] = $ex . ' ' . $sisow->errorMessage;
			//header("Location: " . $this->url->link('checkout/checkout'));
		}
		else {
			$json['success'] = $sisow->issuerUrl;
			//header("Location: " . $sisow->issuerUrl);
		}
		$this->load->library('json');
		$this->response->setOutput(Json::encode($json));
	}
}
?>
